package com.reebake.ideal.message.sse;

import com.reebake.ideal.holder.UserContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

@RestController
@RequestMapping("/sse")
public class SseController {
    @Autowired
    private SseEmitterServer sseEmitterServer;

    @GetMapping(value = "/connect/public", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public SseEmitter connectPublic(@RequestParam("clientId") String clientId) {
        return sseEmitterServer.connect(clientId);
    }

    @GetMapping(value = "/connect/private", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public SseEmitter connect() {
        String userId = UserContextHolder.getCurrentUserId();
        return sseEmitterServer.connect(userId);
    }

}
